import React from 'react';
import { Table } from 'antd';
import Api from 'Utils/api';
import moment from 'moment/moment';
import ImportData from './../Component/ImportData';
import DownloadData from './../Component/DownloadData';
import '../index.scss';

const PAGE_SIZE = 5;
class PunishData extends React.PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      data: [],
      dataCount: 0,
      currentPage: 1,
      pageSise: PAGE_SIZE,
    };
    this.init = ::this.init;
  }
  componentDidMount() {
    this.init();
  }
  init() {
    const api = new Api({
      method: 'GET',
      path: '/enterprise_details/justice_case',
      query: {
        limit: PAGE_SIZE,
        offset: (this.state.currentPage - 1) * PAGE_SIZE,
      },
      success: (json) => {
        this.setState({
          data: json.data_list,
          dataCount: json.total_count,
        });
      }
    });
    api.run();
  }
  generatorColumns() {
    return [
      {
        title: '序号',
        dataIndex: 'id',
        render: text => (
          <span>{text}</span>
        )
      },
      {
        title: '案例名称',
        width: 300,
        dataIndex: 'name',
        render: text => (
          <span >{text}</span >
        )
      },
      {
        title: '处罚决定文书号',
        dataIndex: 'case_code',
        render: text => (
          <span >{text}</span >
        )
      },
      {
        title: '处罚时间',
        dataIndex: 'case_date',
        render: text => (
          <span>{moment(text).format('YYYY-MM-DD')}</span >
        )
      },
      {
        title: '罚没款数（元）',
        dataIndex: 'fine',
        render: text => (
          <span>{text}</span >
        )
      },
      {
        title: '办案机构',
        dataIndex: 'dept_name',
        render: text => (
          <span>{text}</span >
        )
      },
      {
        title: '经办人',
        dataIndex: 'peopole',
        render: (text, record) => (
          <span>{record.staffs}</span>
        )
      }
    ];
  }
  onListPageChange(page) {
    this.setState({ currentPage: page }, () => {
      this.init();
    });
  }
  render() {
    const columns = this.generatorColumns();
    return (
      <div className="data-import-wrap">
        <div className="btns-wrap">
          <DownloadData
            fileName="行政处罚数据模板.csv"
          />
          <ImportData
            path="/justices/case"
            init={this.init}
          />
        </div>
        <div className="data-table-wrap">
          <Table
            rowKey={record => record.id}
            columns={columns}
            bordered
            dataSource={this.state.data}
            pagination={{
              pageSize: this.state.pageSise,
              total: this.state.dataCount,
              current: this.state.currentPage,
              onChange: this.onListPageChange.bind(this)
            }}
          />
        </div>
      </div>
    );
  }
}

export default PunishData;
